//
//  MBHomeViewModel.m
//  MemberBusiness-ios
//
//  Created by cherry pu on 2018/4/24.
//  Copyright © 2018年 zhangXin. All rights reserved.
//

#import "MBHomeViewModel.h"
#import "MBHomeNetTool.h"
#import "MBOrderNetTool.h"
#import "MBShippingModel.h"

@interface MBHomeViewModel ()
@property (nonatomic,strong) MBHomeSellerModel* dataModel;

@end

@implementation MBHomeViewModel

-(void)setUpData
{
    [super setUpData];
    
    MBLoginUserModel* userModle = [MBLoginUserModelArchieveTool userInfoUnAchieveFromFile];
    [self getShippingWithStoreId:userModle.store_id];
    
    [self sendGetVersion];
}

-(void)sendGetVersion
{
    [MBHomeNetTool homeNetToolPostCurrentVersionSuccess:^(MBHTTPResponse *response) {
        if (response.status == 1) {
            NSString* version = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"];
            NSArray* versionArray = [version componentsSeparatedByString:@"."];
            
            NSString* nVersion = response.result;
            NSArray* nVersionArray = [nVersion componentsSeparatedByString:@"."];
            BOOL update = false;
            for (int i=0; i<versionArray.count; i++) {
                int v = [versionArray[i] intValue];
                int nv = [nVersionArray[i] intValue];
                if (v < nv) {
                    update = true;
                    break;
                }
            }
            if (update) {
                self.isNeedUpdate = YES;
            }
        }
    } failure:^(NSError *error) {}];
}

-(void)sendRequest:(MBRequestResult)result
{
    MBLoginUserModel* userModle = [MBLoginUserModelArchieveTool userInfoUnAchieveFromFile];
    [MBHomeNetTool homeNetToolHomeMessageWithStoreId:userModle.store_id success:^(MBHTTPResponse *response) {
        if (response.status == 1) {
            MBHomeSellerModel* model = [MBHomeSellerModel yy_modelWithDictionary:response.result];
            self.dataModel = model;
            result(YES);
        }else {
            result(NO);
            [InterfaceHUD showErrorHUDWithString:response.msg];
        }
    } failure:^(NSError *error) {
        result(NO);
        [InterfaceHUD showErrorHUDWithString:@"店铺数据获取失败，请稍后重试！"];
    }];
}

-(void)getShippingWithStoreId:(NSInteger)storeId
{
    //    店铺物流
    [MBOrderNetTool netToolPostStoreShippingStoreId:storeId success:^(MBHTTPResponse *response) {
        [MBUtil runInGlobalQueue:^{
            if (response.status == 1) {
//                NSArray* array = [response.result objectForKey:@"shipping"];
                NSArray* array = response.result;
                NSMutableArray* list = [NSMutableArray array];
                if (![array isKindOfClass:[NSArray class]]) {
                    return ;
                }
                
                for (int i =0; i<array.count; i++) {
                    MBShippingModel* model = [MBShippingModel yy_modelWithDictionary:array[i]];
                    [list addObject:model];
                }
                [MBLoginUserModelArchieveTool shippingAchieveToFile:list];
            }else {
                [self getShippingWithStoreId:storeId];
            }
        }];
    } failure:^(NSError *error) {
        [MBUtil runInGlobalQueue:^{
            [self getShippingWithStoreId:storeId];
        }];
    }];
}

@end
